package com.boot.security.server.service.impl.mffl.MfflChatContent;

import com.boot.security.server.dao.mffl.MfflChatContentDao;
import com.boot.security.server.dao.mffl.MfflMappingDao;
import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.model.mffl.MfflChatContent;
import com.boot.security.server.model.mffl.MfflMapping;
import com.boot.security.server.model.mffl.MfflUser;
import com.boot.security.server.model.mffl.Vo.MfflMappingVo;
import com.boot.security.server.utils.mffl.JPush.JPushUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class MfflChatContentService {


    @Autowired
    private MfflChatContentDao mfflChatContentDao;

    @Autowired
    private MfflUserDao mfflUserDao;

    @Autowired
    private MfflMappingDao mfflMappingDao;

    /**
     * @param sendId    发送人id
     * @param receiveId 接收人id
     * @param content   消息内容
     */
    public ReturnBean sendMessage(int sendId, int receiveId, String content) {

        MfflUser receiveUser = this.mfflUserDao.getUserInfoAll(receiveId);
        if (receiveUser == null) {
            return new ReturnBean("201", "用户不存在");
        }
        if (receiveUser.getIdentity() == 1 && receiveUser.getLawyerInspect() != 1) {
            return new ReturnBean("201", "该律师为未认证或重新修改认证信息暂不可聊天");
        }

        if (StringUtils.isEmpty(content)) {
            return new ReturnBean("201", "请输入内容");
        }

        MfflMapping mfflMapping = this.mfflMappingDao.getMfflMappingByUserIdAndLawyerId((long) sendId, (long) receiveId);
        if (mfflMapping == null) {
            return new ReturnBean("201", "聊天关系不存在");
        }
        if (mfflMapping.getFirstTime() <= 0) {
            return new ReturnBean("201", "聊天时间已到不能聊天");
        }

        MfflChatContent mfflChatContent = new MfflChatContent();
        mfflChatContent.setSendId(sendId);
        mfflChatContent.setReceiveId(receiveId);
        mfflChatContent.setContent(content);
        mfflChatContent.setMappingId(mfflMapping.getId().intValue());
        mfflChatContent.setState(0);
        int status = this.mfflChatContentDao.save(mfflChatContent);
        if (status == 0) {
            return new ReturnBean("201", "发送失败");
        }
        if (!StringUtils.isEmpty(receiveUser.getJpush())) {
            if (receiveUser.getIdentity() == 0) {
                JPushUtil.pushMsg(receiveUser.getJpush(), "法智通知", content);
            } else if (receiveUser.getIdentity() == 1) {
                JPushUtil.pushMsgLawyer(receiveUser.getJpush(), "法智通知", content);
            }

        }
        this.mfflMappingDao.updateContent(content, mfflMapping.getId());

        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(content);
        return returnBean;
    }


    /**
     * 添加聊天关系
     *
     * @param userId
     * @param receiveId
     */
    public long addMapping(int userId, int receiveId) {
        //添加聊天关系
        MfflMapping mfflMapping = new MfflMappingVo();
        mfflMapping.setUserId(userId);
        mfflMapping.setLawyerId(receiveId);
        mfflMapping.setFirstTime(600);
        mfflMapping.setStartTime(new Date());
        mfflMapping.setFinallyTime(new Date());
        this.mfflMappingDao.save(mfflMapping);
        return mfflMapping.getId();
    }

}
